package com.mymail.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mymail.entity.MailBox;
import com.mymail.entity.User;

public class MailboxDAO extends DAOImp implements DAO {

	@SuppressWarnings("unchecked")
	public ArrayList<MailBox> getMailboxsByUser(User user) {
		return findArray("select * from  `mailbox_detail` where user_id=?",
				new Object[] { user.getId() });
	}

	public boolean getByID(Integer id, Object obj) {
		MailBox mailbox = (MailBox) obj;
		String sql = "select * from `mailbox_detail` where mailbox_id=?";
		return findOne(sql, new Object[] { id }, mailbox);
	}

	public boolean addNew(Object obj) {
		MailBox mailbox = (MailBox) obj;
		String sql = "{ call insert_mailbox(?,?,?,?,?,?) }";
		int i= exeProcedure1(sql, new Object[] { mailbox.getName(),
				mailbox.getPassword(), mailbox.getLasttime(),
				mailbox.getServer_name(), mailbox.getUser() });
		if(i>0){
			mailbox.setId(i);
			return true;
		}
		return false;
	}

	public boolean delete(Object obj) {
		MailBox mailbox = (MailBox) obj;
		String sql = "{ call update_mailbox(?,?,?,?,?) }";
		return exeProcedure(sql, new Object[] { mailbox.getName(),
				mailbox.getPassword(), mailbox.getLasttime(),
				mailbox.getServer_name(), mailbox.getId() });

	}

	public boolean toDatabase(Object obj) {
		MailBox mailbox = (MailBox) obj;
		if (mailbox.getId() == null) {
			return addNew(mailbox);
		} else {
			return update(mailbox);
		}
	}

	public boolean update(Object obj) {
		MailBox mailbox = (MailBox) obj;
		String sql = "update mailbox set mailbox_name=?,mailbox_pass=?,"
				+ "mailbox_lasttime=? where mailbox_id=?";
		return exe(sql, new Object[] { mailbox.getName(),
				mailbox.getPassword(), mailbox.getLasttime(), mailbox.getId() });
	}

	protected MailBox getEntity(ResultSet result) throws SQLException {
		MailBox mailbox = new MailBox();
		setAtribute(result, mailbox);
		return mailbox;
	}

	protected void setAtribute(ResultSet result, Object obj)
			throws SQLException {
		MailBox mailbox = (MailBox) obj;
		mailbox.setId(result.getInt("mailbox_id"));
		mailbox.setUser(result.getInt("user_id"));
		mailbox.setName(result.getString("mailbox_name"));
		mailbox.setPassword(result.getString("mailbox_pass"));
		mailbox.setServer_name(result.getString("server_name"));
		mailbox.setServer_pop3(result.getString("server_pop3"));
		mailbox.setServer_smtp(result.getString("server_smtp"));
		mailbox.setServer_ssl(result.getString("server_ssl").equals("Y"));

	}

}

/*
 * 
 * CREATE VIEW `mailbox_detail`AS select `mailbox`.`mailbox_id` AS `mailbox_id`,
 * `mailbox`.`mailbox_name` AS `mailbox_name`, `mailbox`.`user_id` AS `user_id`,
 * `mailbox`.`mailbox_pass` AS `mailbox_pass`, `mailbox`.`mailbox_lasttime` AS
 * `mailbox_lasttime`, `server`.`server_name` AS `server_name`,
 * `server`.`server_smtp` AS `server_smtp`, `server`.`server_pop3` AS
 * `server_pop3`, `server`.`server_ssl` AS `server_ssl` from (`mailbox` join
 * `server`) where (`mailbox`.`mailbox_server` = `server`.`server_id`);
 */
